package com.newviews.tcmip.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.newviews.tcmip.common.config.ConfigProperties;
import com.newviews.tcmip.common.constant.CombinConsts;
import com.newviews.tcmip.common.constant.CommonConsts;
import com.newviews.tcmip.common.model.PageModel;
import com.newviews.tcmip.common.model.ResultModel;
import com.newviews.tcmip.common.utils.CSVUtils;

import com.newviews.tcmip.common.utils.UUIDGenerator;
import com.newviews.tcmip.dao.EtcmPxExMapper;
import com.newviews.tcmip.dao.EtcmPxMapper;
import com.newviews.tcmip.pojo.po.*;
import com.newviews.tcmip.dao.EtcmDiseaseExMapper;
import com.newviews.tcmip.service.inf.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.newviews.tcmip.web.vo.combin.HerbHerbPageReqVo;
import com.newviews.tcmip.web.vo.combin.HerbHerbReqVo;
import com.newviews.tcmip.web.vo.combin.NetworkStatisticsRespVo;
import com.newviews.tcmip.web.vo.combin.SearchRespVo;
import com.newviews.tcmip.web.vo.etcmherb.HerbIngrVo;
import com.newviews.tcmip.web.vo.etcmpx.PxNetworkStatisticsRespVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 疾病扩展信息 服务实现类
 * </p>
 *
 * @author lxc
 * @since 2024-03-01
 */
@Service
public class EtcmDiseaseExServiceImpl extends ServiceImpl<EtcmDiseaseExMapper, EtcmDiseaseExPo> implements EtcmDiseaseExServiceInf {
    @Resource
    private EtcmDiseaseExMapper diseaseExMapper;
    @Resource
    private EtcmPxServiceInf etcmPxServiceInf;

    @Resource
    private EtcmPxExMapper etcmPxExMapper;
    @Resource
    private ConfigProperties configProperties;

    @Resource
    private EtcmHerbServiceInf etcmHerbServiceInf;

    @Resource
    private EtcmIngrServiceInf etcmIngrServiceInf;
    @Resource
    private EtcmDiseaseServiceInf etcmDiseaseServiceInf;
    @Override
    public ResultModel<NetworkStatisticsRespVo> getIngrIngr(HerbHerbReqVo reqVo) {
        HerbHerbPageReqVo pageReqVo = new HerbHerbPageReqVo();
        BeanUtils.copyProperties(reqVo,pageReqVo);
        ResultModel<PxNetworkStatisticsRespVo> data = etcmPxServiceInf.getNetwork_ingr_ingr(pageReqVo, CommonConsts.STATUS_NORMAL);
//        ResultModel<PxNetworkStatisticsRespVo> data = etcmPxServiceInf.getNetwork(pageReqVo,false,false,false,false,false,true,false, CommonConsts.STATUS_NORMAL);
        if (!data.isSuccess()){
            return new ResultModel<NetworkStatisticsRespVo>().failure(data.getMessage());
        }
        NetworkStatisticsRespVo res = new NetworkStatisticsRespVo();
        res.setScatterDiagram(data.getData().getScatterDiagram());
        return new ResultModel<NetworkStatisticsRespVo>().success(res);
    }

    @Override
    public ResultModel<NetworkStatisticsRespVo> getPxDisease(HerbHerbReqVo reqVo) {
        HerbHerbPageReqVo pageReqVo = new HerbHerbPageReqVo();
        BeanUtils.copyProperties(reqVo,pageReqVo);
//        ResultModel<PxNetworkStatisticsRespVo> data = etcmPxServiceInf.getNetwork(pageReqVo, false, true, true, true, true, true, true, CommonConsts.STATUS_NORMAL);
        ResultModel<PxNetworkStatisticsRespVo> data = etcmPxServiceInf.getNetwork(pageReqVo, false, true, true, true, true, true, true, CommonConsts.STATUS_NORMAL);
        if (!data.isSuccess()){
            return new ResultModel<NetworkStatisticsRespVo>().failure(data.getMessage());
        }
        NetworkStatisticsRespVo res = new NetworkStatisticsRespVo();
        res.setScatterDiagram(data.getData().getScatterDiagram());
        return new ResultModel<NetworkStatisticsRespVo>().success(res);

    }

    @Override
    public ResultModel<NetworkStatisticsRespVo> getIngrDisease(HerbHerbReqVo reqVo) {
        HerbHerbPageReqVo pageReqVo = new HerbHerbPageReqVo();
        BeanUtils.copyProperties(reqVo,pageReqVo);
//        ResultModel<PxNetworkStatisticsRespVo> data = etcmPxServiceInf.getNetwork(pageReqVo, false, false, true, false, false, true, false, CommonConsts.STATUS_NORMAL);
        ResultModel<PxNetworkStatisticsRespVo> data = etcmPxServiceInf.getNetwork(pageReqVo, false, false, true, false, false, true, false, CommonConsts.STATUS_NORMAL);
        if (!data.isSuccess()){
            return new ResultModel<NetworkStatisticsRespVo>().failure(data.getMessage());
        }
        NetworkStatisticsRespVo res = new NetworkStatisticsRespVo();
        res.setScatterDiagram(data.getData().getScatterDiagram());
        return new ResultModel<NetworkStatisticsRespVo>().success(res);
    }

    @Override
    public ResultModel<NetworkStatisticsRespVo> getHerbDisease(HerbHerbReqVo reqVo) {
        HerbHerbPageReqVo pageReqVo = new HerbHerbPageReqVo();
        BeanUtils.copyProperties(reqVo,pageReqVo);
//        ResultModel<PxNetworkStatisticsRespVo> data = etcmPxServiceInf.getNetwork(pageReqVo, false, true, true, false, true, true, true, CommonConsts.STATUS_NORMAL);
        ResultModel<PxNetworkStatisticsRespVo> data = etcmPxServiceInf.getNetwork(pageReqVo, false, true, true, false, true, true, true, CommonConsts.STATUS_NORMAL);
        if (!data.isSuccess()){
            return new ResultModel<NetworkStatisticsRespVo>().failure(data.getMessage());
        }
        NetworkStatisticsRespVo res = new NetworkStatisticsRespVo();
        res.setScatterDiagram(data.getData().getScatterDiagram());
        return new ResultModel<NetworkStatisticsRespVo>().success(res);
    }



    @Override
    public ResultModel<PageModel<Map<String, String>>> getIngrIngrPage(HerbHerbPageReqVo reqVo) {
        ResultModel<PxNetworkStatisticsRespVo> data = etcmPxServiceInf.getNetwork_ingr_ingr(reqVo, CommonConsts.STATUS_DEL);
//        ResultModel<PxNetworkStatisticsRespVo> data = etcmPxServiceInf.getNetwork(reqVo,false,false,false,false,false,true,false, CommonConsts.STATUS_DEL);
        if (!data.isSuccess()){
            return new ResultModel<PageModel<Map<String, String>>>().failure(data.getMessage());
        }
        return new ResultModel<PageModel<Map<String, String>>>().success(data.getData().getTableList());
    }

    @Override
    public ResultModel<PageModel<Map<String, String>>> getPxDiseasePage(HerbHerbPageReqVo reqVo) {
//        ResultModel<PxNetworkStatisticsRespVo> data = etcmPxServiceInf.getNetwork(reqVo, false, true, true, true, true, true, true, CommonConsts.STATUS_DEL);
        ResultModel<PxNetworkStatisticsRespVo> data = etcmPxServiceInf.getNetwork(reqVo, false, true, true, true, true, true, true, CommonConsts.STATUS_DEL);
        if (!data.isSuccess()){
            return new ResultModel<PageModel<Map<String, String>>>().failure(data.getMessage());
        }
        return new ResultModel<PageModel<Map<String, String>>>().success(data.getData().getTableList());
    }

    @Override
    public ResultModel<PageModel<Map<String, String>>> getHerbDiseasePage(HerbHerbPageReqVo reqVo) {
//        ResultModel<PxNetworkStatisticsRespVo> data = etcmPxServiceInf.getNetwork(reqVo, false, true, true, false, true, true, true, CommonConsts.STATUS_DEL);
        ResultModel<PxNetworkStatisticsRespVo> data = etcmPxServiceInf.getNetwork(reqVo, false, true, true, false, true, true, true, CommonConsts.STATUS_DEL);
        if (!data.isSuccess()){
            return new ResultModel<PageModel<Map<String, String>>>().failure(data.getMessage());
        }
        return new ResultModel<PageModel<Map<String, String>>>().success(data.getData().getTableList());
    }

    @Override
    public ResultModel<PageModel<Map<String, String>>> getIngrDiseasePage(HerbHerbPageReqVo reqVo) {
//        ResultModel<PxNetworkStatisticsRespVo> data = etcmPxServiceInf.getNetwork(reqVo, false, false, true, false, false, true, false, CommonConsts.STATUS_DEL);
        ResultModel<PxNetworkStatisticsRespVo> data = etcmPxServiceInf.getNetwork(reqVo, false, false, true, false, false, true, false, CommonConsts.STATUS_DEL);
        if (!data.isSuccess()){
            return new ResultModel<PageModel<Map<String, String>>>().failure(data.getMessage());
        }
        return new ResultModel<PageModel<Map<String, String>>>().success(data.getData().getTableList());
    }

    @Override
    public void getDownload(HttpServletResponse response, String uiTypeCode, String langCode, String exportType, List<Map<String, String>> list) {
        String path = "";
        try {
            String suffix = "";
            String uiCode = "";
            if (uiTypeCode.equals(CommonConsts.UI_TYPE_CODE_PX_INGR)){
                uiCode = CommonConsts.UI_TYPE_CODE_INGR;
            }
            if (uiTypeCode.equals(CommonConsts.UI_TYPE_CODE_HERB_INGR)){
                uiCode = CommonConsts.UI_TYPE_CODE_INGR;
            }
            List<EtcmUiElPo> uiElName = etcmPxExMapper.getUIElName(StringUtils.isNotBlank(uiCode) ? uiCode : uiTypeCode, langCode);
            if (CommonConsts.UI_HERB.equals(uiTypeCode)){
                uiElName = uiElName.stream().filter(etcmUiElPo -> etcmUiElPo.getUiElSort() != 3).sorted(Comparator.comparing(EtcmUiElPo::getUiElSort)).collect(Collectors.toList());
            }
            if (uiTypeCode.equals(CommonConsts.UI_TYPE_CODE_INGR)){
                uiElName = uiElName.stream().filter(s -> s.getUiElSort() < 7).sorted(Comparator.comparing(EtcmUiElPo::getUiElSort)).collect(Collectors.toList());
            }
            if (uiTypeCode.equals(CommonConsts.UI_TYPE_CODE_PX_INGR)){
                uiElName = uiElName.stream().filter(s -> s.getUiElSort() < 3 || (s.getUiElSort() > 6  && s.getUiElSort() < 12)).sorted(Comparator.comparing(EtcmUiElPo::getUiElSort)).collect(Collectors.toList());
            }
            if (uiTypeCode.equals(CommonConsts.UI_TYPE_CODE_HERB_INGR)){
                uiElName = uiElName.stream().filter(s -> s.getUiElSort() < 3 || (s.getUiElSort() > 6  && s.getUiElSort() < 12)).sorted(Comparator.comparing(EtcmUiElPo::getUiElSort)).collect(Collectors.toList());
            }
            LinkedHashMap<String,String> map = new LinkedHashMap<>(16);
            for (EtcmUiElPo etcmUiElPo : uiElName) {
                map.put(etcmUiElPo.getUiElCode(),etcmUiElPo.getUiElName());
            }

            if(exportType.equals(CommonConsts.STR_COMMON_YES)){
                suffix = CommonConsts.EXPORT_FILE_FORMAT_1;

            }else {
                suffix = CommonConsts.EXPORT_FILE_FORMAT_2;

            }
            File csvFile = CSVUtils.createCSVFile(list, map, configProperties.getFileExportPath(), UUIDGenerator.getDateNumber(),suffix);
           // CSVUtils.download(csvFile.getPath(), response);
            path = csvFile.getPath();
            InputStream stream = new FileInputStream(csvFile.getPath());
            response.setHeader("Content-Disposition", "attachment;"+"filename=" + new String(csvFile.getName().getBytes(), "utf-8"));
            ServletOutputStream out = response.getOutputStream();
            byte buff[] = new byte[1024];
            int length = 0;
            while ((length = stream.read(buff)) > 0) {
                out.write(buff, 0, length);
            }
            stream.close();
            out.close();
            out.flush();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            new File(path).delete();
        }
    }


    @Override
    public ResultModel<List<SearchRespVo>> getHerbSearch(String name, String langCode,Integer type) {
        List<SearchRespVo> listRes = new ArrayList<>();
        switch (type){
            case CommonConsts.PX:
                QueryWrapper<EtcmPxPo> pxWrapper = new QueryWrapper<>();

                pxWrapper.and(w->w.like( "px_zh_name", name)
                        .or().like( "px_zh_name_py", name)
                        .or().like( "px_zh_name_wb", name)
                        .or().like( "px_zh_name_abb", name)
                        .or().like( "px_en_name", name)
                        .or().like( "px_en_name_abb", name)
                        .or().like( "px_latin_name", name)
                        .or().like( "px_latin_name_abb", name)
                        .or().like( "px_name_alias", name)
                );
                if (langCode.equals(CommonConsts.LANG_CODE_ENG)){
                    pxWrapper.isNotNull("px_zh_name_py");
                }
                if (langCode.equals(CommonConsts.LANG_CODE_CHN)){
                    pxWrapper.isNotNull("px_en_name");
                }
                pxWrapper.last("limit 100");
                List<EtcmPxPo> pxList = etcmPxServiceInf.list(pxWrapper);
                for (EtcmPxPo etcmPxPo : pxList) {
                    SearchRespVo searchRespVo = new SearchRespVo();
                    searchRespVo.setCode(etcmPxPo.getPxCode());

                    if (langCode.equals(CommonConsts.LANG_CODE_ENG)){
                        searchRespVo.setName(etcmPxPo.getPxZhNamePy());

                    }
                    if (langCode.equals(CommonConsts.LANG_CODE_CHN)){
                        searchRespVo.setName(etcmPxPo.getPxZhName());
                    }
                    listRes.add(searchRespVo);
                }
                break;
            case CommonConsts.HERB:
                QueryWrapper<EtcmHerbPo> herbWrapper = new QueryWrapper<>();
                herbWrapper.and(w->w.like( "herb_zh_name", name)
                        .or().like( "herb_zh_name_py", name)
                        .or().like( "herb_zh_name_wb", name)
                        .or().like( "herb_zh_name_abb", name)
                        .or().like( "herb_en_name", name)
                        .or().like( "herb_en_name_abb", name)
                        .or().like( "herb_latin_name", name)
                        .or().like( "herb_latin_name_abb", name)
                        .or().like( "herb_name_alias", name)
                );
                if (langCode.equals(CommonConsts.LANG_CODE_ENG)){
                    herbWrapper.isNotNull("herb_en_name");
                }
                if (langCode.equals(CommonConsts.LANG_CODE_CHN)){
                    herbWrapper.isNotNull("herb_zh_name");
                }
                herbWrapper.last("limit 100");
                List<EtcmHerbPo> herbList = etcmHerbServiceInf.list(herbWrapper);
                for (EtcmHerbPo etcmHerbPo : herbList) {
                    SearchRespVo searchRespVo = new SearchRespVo();
                    searchRespVo.setCode(etcmHerbPo.getHerbCode());
                    if (StringUtils.isBlank(etcmHerbPo.getHerbEnName()) && StringUtils.isBlank(etcmHerbPo.getHerbZhName())){
                        continue;
                    }
                    if (langCode.equals(CommonConsts.LANG_CODE_ENG)){
                        searchRespVo.setName(etcmHerbPo.getHerbEnName());
                    }
                    if (langCode.equals(CommonConsts.LANG_CODE_CHN)){
                        searchRespVo.setName(etcmHerbPo.getHerbZhName());
                    }
                    listRes.add(searchRespVo);
                }
                break;
            case CommonConsts.INGREDIENT:
                QueryWrapper<EtcmIngrPo> ingrWrapper = new QueryWrapper<>();
                ingrWrapper.and(w->w.like( "ingr_zh_name", name)
                        .or().like( "ingr_zh_name_py", name)
                        .or().like( "ingr_zh_name_wb", name)
                        .or().like( "ingr_zh_name_abb", name)
                        .or().like( "ingr_en_name", name)
                        .or().like( "ingr_en_name_abb", name)
                        .or().like( "ingr_latin_name", name)
                        .or().like( "ingr_latin_name_abb", name)
                        .or().like( "ingr_name_alias", name)
                );
                if (langCode.equals(CommonConsts.LANG_CODE_ENG)){
                    ingrWrapper.isNotNull("ingr_en_name");
                }
                if (langCode.equals(CommonConsts.LANG_CODE_CHN)){
                    ingrWrapper.isNotNull("ingr_zh_name");
                }
                ingrWrapper.last("limit 100");
                List<EtcmIngrPo> ingrList = etcmIngrServiceInf.list(ingrWrapper);
                for (EtcmIngrPo etcmIngrPo : ingrList) {
                    SearchRespVo searchRespVo = new SearchRespVo();
                    searchRespVo.setCode(etcmIngrPo.getIngrCode());
                    if (StringUtils.isBlank(etcmIngrPo.getIngrEnName()) && StringUtils.isBlank(etcmIngrPo.getIngrZhName())){
                        continue;
                    }
                    if (langCode.equals(CommonConsts.LANG_CODE_ENG)){
                        searchRespVo.setName(etcmIngrPo.getIngrEnName());
                    }
                    if (langCode.equals(CommonConsts.LANG_CODE_CHN)){
                        searchRespVo.setName(etcmIngrPo.getIngrZhName());
                    }
                    listRes.add(searchRespVo);
                }
                break;
            case CommonConsts.DISEASE:
                QueryWrapper<EtcmDiseasePo> diseaseWrapper = new QueryWrapper<>();
                diseaseWrapper.and(w->w.like( "disease_zh_name", name)
                        .or().like( "disease_zh_name_py", name)
                        .or().like( "disease_zh_name_wb", name)
                        .or().like( "disease_zh_name_abb", name)
                        .or().like( "disease_en_name", name)
                        .or().like( "disease_en_name_abb", name)
                        .or().like( "disease_latin_name", name)
                        .or().like( "disease_latin_name_abb", name)
                        .or().like( "disease_name_alias", name)
                );
                if (langCode.equals(CommonConsts.LANG_CODE_ENG)){
                    diseaseWrapper.isNotNull("disease_en_name");
                }
                if (langCode.equals(CommonConsts.LANG_CODE_CHN)){
                    diseaseWrapper.isNotNull("disease_zh_name");
                }
                diseaseWrapper.last("limit 100");
                List<EtcmDiseasePo> diseaseList = etcmDiseaseServiceInf.list(diseaseWrapper);

                for (EtcmDiseasePo etcmDiseasePo : diseaseList) {
                    SearchRespVo searchRespVo = new SearchRespVo();
                    searchRespVo.setCode(etcmDiseasePo.getDiseaseCode());
                    if (StringUtils.isBlank(etcmDiseasePo.getDiseaseEnName()) && StringUtils.isBlank(etcmDiseasePo.getDiseaseZhName())){
                        continue;
                    }
                    if (langCode.equals(CommonConsts.LANG_CODE_ENG)){
                        searchRespVo.setName(etcmDiseasePo.getDiseaseEnName());
                    }
                    if (langCode.equals(CommonConsts.LANG_CODE_CHN)){
                        searchRespVo.setName(etcmDiseasePo.getDiseaseZhName());
                    }
                    listRes.add(searchRespVo);
                }
            default:
        }

        return new ResultModel<List<SearchRespVo>>().success(listRes);
    }

}
